/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import com.crayon.account.dao.DAOTable;
import com.crayon.account.loader.LoaderSQLTableAppend;
import com.crayon.account.loader.LoaderSQLTables;
import com.crayon.sharemenu.R;
import com.crayon.widget.DraggableGridView;
import com.crayon.widget.OnRearrangeListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class FragmentChildTableAdd extends Fragment
{

	public static FragmentChildTableAdd onCrateInstance(Date time){
		FragmentChildTableAdd f = new FragmentChildTableAdd();
		Bundle args =new Bundle();
//		args.putLong(PARAM_DATE, time.getTime());
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_child_table_add,container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
        dgv = ((DraggableGridView)getView().findViewById(R.id.vgv));
        button1 = ((Button)getView().findViewById(R.id.button1));
        button2 = ((Button)getView().findViewById(R.id.button2));

        setListeners();
        callLoaderAll();
	}

    private void callLoaderAll(){
        LoaderSQLTables loaderSQLTables = new LoaderSQLTables(getActivity());
        loaderSQLTables.registerListener(0x00, new Loader.OnLoadCompleteListener<List<DAOTable>>() {
            @Override
            public void onLoadComplete(Loader<List<DAOTable>> loader, List<DAOTable> data) {

                if(getActivity()==null) return;
                if (data != null && data.size() > 0) {
                    poem.clear();
                    for (int i = 0; i < dgv.getChildCount(); i++) {
                        dgv.removeViewAt(i);
                    }

                    for (DAOTable item : data) {
                        String word = item.getTableName();
                        ImageView view = new ImageView(getActivity());
                        if(view!=null){
                            view.setImageBitmap(getThumb(word));
                            dgv.addView(view);
                            poem.add(item);

                        }
                    }
                }
            }
        });
        loaderSQLTables.forceLoad();
    }
    static Random random = new Random();
    static String[] words = "개나리 국화 장미 모란 엉겅 봉선화 능소화 은방울 찔레 라일락 수선화 튤립".split(" ");
    DraggableGridView dgv;
    Button button1, button2;
    ArrayList<DAOTable> poem = new ArrayList<DAOTable>();
    private void setListeners()
    {
        dgv.setOnRearrangeListener(new OnRearrangeListener() {
            public void onRearrange(int oldIndex, int newIndex) {
                DAOTable word = poem.remove(oldIndex);
                if (oldIndex < newIndex)
                    poem.add(newIndex, word);
                else
                    poem.add(newIndex, word);


//                dgv.getChil
                new LoaderSQLTableAppend(getActivity(),poem).forceLoad();


            }
        });
        dgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//                dgv.removeViewAt(arg2);
//                poem.remove(arg2);

                //상세 보기...
            }
        });
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                String word = String.format("%2d %s",dgv.getChildCount()+1,words[random.nextInt(words.length)]);
                ImageView view = new ImageView(getActivity());
                view.setImageBitmap(getThumb(word));
                dgv.addView(view);
                DAOTable item =new DAOTable();
                item.setTableName(word);
                item.setOrder(dgv.getChildCount());

                poem.add(item);

                new LoaderSQLTableAppend(getActivity(),item).forceLoad();

            }
        });
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

//                dgv.g
            }
        });
    }

    private Bitmap getThumb(String s)
    {
        Bitmap bmp = Bitmap.createBitmap(150, 150, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColor(Color.rgb(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
        paint.setTextSize(30);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        canvas.drawRect(new Rect(0, 0, 150, 150), paint);
        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(s, 75, 75, paint);

        return bmp;
    }

}
